
* Prepare data for gains from marriage (include irregulars) - local labor markets as reference 

use "$data\M_final_dataset.dta" , clear

rename y_survey year
gen hnative= hbc==0
tab hnative
gen wnative= wbc==0
tab wnative 
gen HOM = hbc==wbc
lab var HOM "Homogamous"
gen HET = HOM!=1
lab var HET "Heterogamous"
gen HET_IT =  (hbc==0 & wbc>0) | ( hbc>0 & wbc==0) 
lab var HET_IT "Heterogamous-Italian"
gen HETW = HET==1 & wbc!=0
gen HETH = HET==1 & hbc!=0

gen famt= .
replace famt=1 if wnative==1 & hnative==1
replace famt=2 if wnative==1 & hnative==0
replace famt=3 if wnative==0 & hnative==1
replace famt=4 if wnative==0 & hnative==0
label define famtl 1 "Wife Native-Husband Native" 2 "Wife Native-Husband Foreign" 3 "Wife Foreign-Husband Native" 4 "Wife Foreign-Husband Foreign"
label values famt famtl

* Merge with local labor market classification

merge m:1 region istat using "$data\LocalLaborMarkets_define.dta" , keep(match master) keepusing(llm)
tab _merge
drop _merge 
codebook llm

* Generate number of marriages

gen nm=1
collapse (sum) nm , by( region llm year wbc hbc HOM HET_IT HET  famt)

* Merge with single data 

merge m:1 llm hbc year using "$data\Msinglemag_country_alltime_llm.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
replace Msingle=round(Msingle,1)

merge m:1 llm wbc year using "$data\Fsinglemag_country_alltime_llm.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
replace Fsingle=round(Fsingle,1)

* Merge with population 

merge m:1 llm using "$data\population_byllm.dta" , keep(match master) 
tab _merge
drop _merge

* Generate gains to marriage CS2006

collapse (sum) nm  (mean) Fsingle Msingle , by( region llm year wbc hbc HOM  HET HET_IT famt population)
gen phixy = ln((nm^2)/(Fsingle*Msingle))
tabstat phixy , by(famt) stat( mean sd N)

lab var phixy "gains to marriage"
save "$data\Gain_marriage_llm.dta" , replace



******************* NATIVE HUSBAND - FOREIGN WIFE **************************

use "$data\Gain_marriage_llm.dta" , clear
keep if hbc==0

* Treatment
gen nat= wbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0  
lab define eui3 0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3

tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Time
tab year
drop if year==1995 
drop if year==1996 
drop if year==1997
tab year, gen(t)
rename t1 t1998
rename t2 t1999
rename t3 t2000
rename t4 t2001
rename t5 t2002
rename t6 t2003
rename t7 t2004
rename t8 t2005
rename t9 t2006
rename t10 t2007
rename t11 t2008
rename t12 t2009
rename t13 t2010
rename t14 t2011
rename t15 t2012
gen post2003= year>=2003
gen post2004= year>=2004
gen post2006= year>=2006
gen post2007= year>=2007

* Interaction
gen eu2004post2003= eu2004*post2003
gen eu2004post2004= eu2004*post2004
gen eu2004post2006= eu2004*post2006
gen eu2004post2007= eu2004*post2007
gen eu2007post2003= eu2007*post2003
gen eu2007post2004= eu2007*post2004
gen eu2007post2006= eu2007*post2006
gen eu2007post2007= eu2007*post2007

forvalues t=1998/2012 {
gen eu2004t`t' = eu2004*t`t'
gen eu2007t`t'  = eu2007*t`t'
}
drop if phixy==.
xtset llm 
order llm year
sort llm year
xtsum phixy 

egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen wbprov = wbc*1000+llm
gen yearprov = year*1000+llm
tab wbc, gen(dwbc)
forval i=1/159 {
gen wbcyear`i' = dwbc`i'*year
}

tab llm, gen(dpl)
forval i=1/`r(r)' {
gen llmtrend`i' = dpl`i'*year
}


********************************************************************************
*  Appendix Table A6 : Column 2
*  Gains from marriage before and after the EU enlargements, DID estimates (robustness with local labor markets)

areg phixy newEUxPostEU i.year llmtrend1-llmtrend581 wbcyear1-wbcyear159 [aw=population], absorb(wbprov) vce(cluster llm)
outreg2 using "$output\baseline_llm.xls" , keep(newEUxPostEU) replace 






******************* NATIVE WIFE - FOREIGN HUSBAND **************************
use "$data\Gain_marriage_llm.dta" , clear
keep if wbc==0

* Treatment
gen nat = hbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3 0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Time 
tab year
drop if year==1995 
drop if year==1996 
drop if year==1997
tab year, gen(t)
rename t1 t1998
rename t2 t1999
rename t3 t2000
rename t4 t2001
rename t5 t2002
rename t6 t2003
rename t7 t2004
rename t8 t2005
rename t9 t2006
rename t10 t2007
rename t11 t2008
rename t12 t2009
rename t13 t2010
rename t14 t2011
rename t15 t2012
gen post2003= year>=2003
gen post2004= year>=2004
gen post2006= year>=2006
gen post2007= year>=2007

* Interaction
gen eu2004post2003= eu2004*post2003
gen eu2004post2004= eu2004*post2004
gen eu2004post2006= eu2004*post2006
gen eu2004post2007= eu2004*post2007
gen eu2007post2003= eu2007*post2003
gen eu2007post2004= eu2007*post2004
gen eu2007post2006= eu2007*post2006
gen eu2007post2007= eu2007*post2007

forvalues t=1998/2012 {
gen eu2004t`t' = eu2004*t`t'
gen eu2007t`t'  = eu2007*t`t'
}

drop if phixy==.
xtset llm 
order llm year
sort llm year
xtsum phixy 

egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen hbprov = hbc*1000+llm
gen yearprov = year*1000+llm
codebook hbc
tab hbc, gen(dhbc)
forval i=1/153 {
gen hbcyear`i' = dhbc`i'*year
}
tab llm, gen(dpl)
forval i=1/`r(r)' {
gen llmtrend`i' = dpl`i'*year
}

********************************************************************************
*  Appendix Table A6 : Column 4
*  Gains from marriage before and after the EU enlargements, DID estimates (robustness with local labor markets)

areg phixy newEUxPostEU i.year llmtrend1-llmtrend581 hbcyear1-hbcyear153 [aw=population], absorb(hbprov) vce(cluster llm)
outreg2 using "$output\baseline_llm.xls" , keep(newEUxPostEU) append 

